//
//  SuperAndSuberViewViewController.m
//  CoreAnimation
//
//  Created by 张国文 on 16/11/1.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "SuperAndSuberViewViewController.h"
#import "TestSuperView.h"
#import "TestSubView.h"

@interface SuperAndSuberViewViewController ()

@property (nonatomic, strong) TestSuperView *mySuperView;

@property (nonatomic, strong) TestSubView *mySuberView;

@end

@implementation SuperAndSuberViewViewController

/**
 *   layerOutSubview会在drawRect方法之前调用。
     因为layerOutSubview是布局子视图用的，这个方法内部隐式设置了子视图应该在父视图的具体位置，因此只有确定这些子视图的具体位置，然后父视图才能够调用drawRect将自身绘制出来。
     因此当子视图的frame大小发生变化的时候，父视图会先调用layerOutSubview对子视图重新进行位置的布局，然后子视图本身调用layerOutSubview对自身的子视图进行重新布局。这个时候drawRect已经不会调用，因为drawRect只是绘制视图在视图第一次加载的时候调用，视图展现出来以后进行的任何更改只是layer层的移动或者放大旋转等操作，drawRect并不需要。
 */
/**
*  结论：视图只有加入父视图中，视图才会调用自身的layoutSubviews和drawRect方法。
父视图和子视图的layoutSubviews全部调用完以后(父视图的先调用)，才会走父视图和子视图的drawRect方法(父视图先调用)，实际上就是按照视图树的数据进行调用了。
*/

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.mySuberView = [[TestSubView alloc] initWithFrame:CGRectMake(50,50 , 100, 100)];
    self.mySuberView.backgroundColor = [UIColor cyanColor];
    
    self.mySuperView = [[TestSuperView alloc] initWithFrame:CGRectMake(80, 80, 200, 200)];
    self.mySuperView.backgroundColor = [UIColor redColor];
    self.mySuperView.contentMode = UIViewContentModeRedraw; // 只有bounds改变才会走drawRect
    [self.view addSubview:_mySuperView];
    
   
    [self.mySuperView addSubview:_mySuberView];
    
    UIButton *changeSuperButton = [UIButton buttonWithType:UIButtonTypeCustom];
    changeSuperButton.frame = CGRectMake(50, 400, 270, 30);
    changeSuperButton.backgroundColor = [UIColor cyanColor];
    [changeSuperButton setTitle:@"更改父视图大小" forState:UIControlStateNormal];
    [changeSuperButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [changeSuperButton addTarget:self action:@selector(changeSuper:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:changeSuperButton];
    
    UIButton *changeSuberButton = [UIButton buttonWithType:UIButtonTypeCustom];
    changeSuberButton.frame = CGRectMake(50, 450, 270, 30);
    changeSuberButton.backgroundColor = [UIColor cyanColor];
    [changeSuberButton setTitle:@"更改子视图大小" forState:UIControlStateNormal];
    [changeSuberButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [changeSuberButton addTarget:self action:@selector(changeSuber:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:changeSuberButton];
    
    UIButton *changeSuperCenterButton = [UIButton buttonWithType:UIButtonTypeCustom];
    changeSuperCenterButton.frame = CGRectMake(50, 500, 270, 30);
    changeSuperCenterButton.backgroundColor = [UIColor cyanColor];
    [changeSuperCenterButton setTitle:@"更改父视图center位置" forState:UIControlStateNormal];
    [changeSuperCenterButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [changeSuperCenterButton addTarget:self action:@selector(changeSuperCenter:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:changeSuperCenterButton];
    
    UIButton *changeSuberCenterButton = [UIButton buttonWithType:UIButtonTypeCustom];
    changeSuberCenterButton.frame = CGRectMake(50, 550, 270, 30);
    changeSuberCenterButton.backgroundColor = [UIColor cyanColor];
    [changeSuberCenterButton setTitle:@"更改子视图大小位置" forState:UIControlStateNormal];
    [changeSuberCenterButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [changeSuberCenterButton addTarget:self action:@selector(changeSuberCenter:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:changeSuberCenterButton];
}

- (void)changeSuper:(UIButton *)button
{
    self.mySuperView.bounds = CGRectMake(0, 0, 250, 250);
    //  [self.mySuperView.layer setNeedsDisplay]; // 关联视图设置为刷新也可以重新调用drawRect
}

- (void)changeSuber:(UIButton *)button
{
    self.mySuberView.bounds = CGRectMake(0, 0, 50, 50);
}

- (void)changeSuperCenter:(UIButton *)button
{
    self.mySuperView.center = CGPointMake(250, 250); // 更改center不会调用drawRect
}

- (void)changeSuberCenter:(UIButton *)button
{
    self.mySuberView.center = CGPointMake(130, 130);
}

@end
